/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.cicp.util.pool;

import org.apache.commons.pool.PoolableObjectFactory;
import org.python.modules.newmodule;

import com.sinodata.bsm.cicp.util.telnet.JtaSSH;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-10-24 下午1:31:14          wangyazhou        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class SSHPoolableObject implements PoolableObjectFactory {

    private String ip;

    private String user;

    private String pwd;

    private int port = 22;

    private String prompt;

    public SSHPoolableObject(String ip, int port, String user, String pwd, String prompt) {
        this.ip = ip;
        this.port = port;
        this.user = user;
        this.pwd = pwd;
        this.prompt = prompt;
    }

    @Override
    public void activateObject(Object arg0) throws Exception {

    }

    @Override
    public void destroyObject(Object obj) throws Exception {
        JtaSSH ssh = (JtaSSH) obj;
        ssh.disconnect();
    }

    @Override
    public Object makeObject() throws Exception {
        JtaSSH ssh = new JtaSSH();
        ssh.connect(ip, port);
        ssh.runLogin(user, pwd, prompt);
        ssh.runTask("LANG=C;export LANG"); //改为英语环境 否则解析shell时会混乱
        return ssh;
    }

    @Override
    public void passivateObject(Object arg0) throws Exception {

    }

    @Override
    public boolean validateObject(Object obj) {
        JtaSSH ssh = (JtaSSH) obj;
        boolean b = ssh.getIsValid();
        return b;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

}
